package com.ydgk.servlet;

import com.ydgk.bean.Supplier;
import com.ydgk.dao.SupplierDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "SupplierPagingServlet", urlPatterns = "/SupplierPagingServlet.action")
public class SupplierPagingServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String pageNumber = request.getParameter("pageNumber");
        SupplierDao supplierDao = new SupplierDao();
        int page = 0;
        try{
            page = Integer.parseInt(pageNumber);
        }catch (NumberFormatException e){
            page = 1;
        }
        int maxPage = supplierDao.maxPage("supplier");
        if(page>maxPage){
            page = maxPage;
        }else if(page<0){
            page = 1;
        }
        List<Supplier> supplierlist = supplierDao.paging(page);
        request.setAttribute("supplierlist",supplierlist);
        request.setAttribute("page",page);
        request.setAttribute("maxPage",maxPage);
        request.getRequestDispatcher("suppliers.jsp").forward(request,response);
    }


}
